/**
 * Copyright 厦门中软海晟信息技术有限公司 版权所有 违者必究 2019
 */
package com.opencvjava.piano.support;

import org.apache.commons.lang3.StringUtils;
import org.opencv.core.Mat;

import java.util.ArrayList;
import java.util.List;

import static com.opencvjava.support.util.CvUtils.imshow;
import static com.opencvjava.support.util.CvUtils.mat;
import static org.opencv.imgcodecs.Imgcodecs.IMREAD_GRAYSCALE;

/**
 * @author : sunzb
 * @date: 2019/11/28
 */
public class ImageInfo {
    private List<Unit> units = new ArrayList<>();
    private Block fillBlock = new Block();
    private List<Block> blocks = new ArrayList<>();
    private int cols;
    private int rows;
    private int total;
    private byte[] data;
    private byte[] dataSrc;
    private Mat gray;
    private Mat src;
    private UnitBuilder unitBuilder;

    public ImageInfo(String file) {
        this(null, file);
    }

    public ImageInfo(String fold, String file) {
        Mat src, gray;
        if (StringUtils.isNotBlank(fold)) {
            src = mat(fold, file);
            gray = mat(fold, file, IMREAD_GRAYSCALE);
        } else {
            src = mat(file);
            gray = mat(file, IMREAD_GRAYSCALE);
        }
        if (src.empty() || gray.empty()) throw new RuntimeException("获取不到图片");
        long total = gray.total();
        if (total < 10000) throw new RuntimeException("图片尺寸太小");
        if (total > 90000000) throw new RuntimeException("图片尺寸太大");
        imshow("源图", src);
        data = new byte[(int) total];
        gray.get(0, 0, data);
        this.setCols(gray.cols())
                .setRows(gray.rows()).setTotal((int) total)
                .setSrc(src).setGray(gray);
        unitBuilder = new UnitBuilder().setImg(this);
    }

    public ImageInfo build() {
        unitBuilder.units();
        unitBuilder.blocks();
        return this;
    }

    public Mat getGray() {
        return gray;
    }

    public ImageInfo setGray(Mat gray) {
        this.gray = gray;
        return this;
    }

    public Mat getSrc() {
        return src;
    }

    public ImageInfo setSrc(Mat src) {
        this.src = src;
        return this;
    }

    public byte[] getDataSrc() {
        return dataSrc;
    }

    public ImageInfo setDataSrc(byte[] dataSrc) {
        this.dataSrc = dataSrc;
        return this;
    }

    public byte[] getData() {
        return data;
    }

    public ImageInfo setData(byte[] data) {
        this.data = data;
        return this;
    }

    public List<Unit> getUnits() {
        return units;
    }

    public ImageInfo setUnits(List<Unit> units) {
        this.units = units;
        return this;
    }

    public List<Block> getBlocks() {
        return blocks;
    }

    public ImageInfo setBlocks(List<Block> blocks) {
        this.blocks = blocks;
        return this;
    }

    public int getCols() {
        return cols;
    }

    public ImageInfo setCols(int cols) {
        this.cols = cols;
        return this;
    }

    public int getRows() {
        return rows;
    }

    public ImageInfo setRows(int rows) {
        this.rows = rows;
        return this;
    }

    public int getTotal() {
        return total;
    }

    public ImageInfo setTotal(int total) {
        this.total = total;
        return this;
    }

    public Block getFillBlock() {
        return fillBlock;
    }

    public ImageInfo setFillBlock(Block fillBlock) {
        this.fillBlock = fillBlock;
        return this;
    }
}
